package com.tc.nio;

import com.tc.util.ByteBufferUtil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Auther: tianchao
 * @Date: 2022/1/30 09:23
 * @Description: 分段读取
 */
public class TestScatteringReads {

    public static void main(String[] args) {
        try (
                FileChannel fileChannel  = new RandomAccessFile("words.txt","r").getChannel();
        ){
            ByteBuffer byteBuffer1 = ByteBuffer.allocate(3);
            ByteBuffer byteBuffer2 = ByteBuffer.allocate(3);
            ByteBuffer byteBuffer3 = ByteBuffer.allocate(3);
            ByteBuffer byteBuffer4 = ByteBuffer.allocate(3);
            ByteBuffer[] byteBuffers = new ByteBuffer[]{byteBuffer1,byteBuffer2,byteBuffer3,byteBuffer4};
            fileChannel.read(byteBuffers);
            byteBuffer1.flip();
            byteBuffer2.flip();
            byteBuffer3.flip();
            byteBuffer4.flip();
            ByteBufferUtil.debugAll(byteBuffer1);
            ByteBufferUtil.debugAll(byteBuffer2);
            ByteBufferUtil.debugAll(byteBuffer3);
            ByteBufferUtil.debugAll(byteBuffer4);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
